package com.zhouyong.leetcode.算出两个日期的相差的天数面试题;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author 99
 * @Date 2022/9/12 12:13
 * @Version 1.0
 */
public class Test {

    public static void main(String[] args) {

        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

            Date a = simpleDateFormat.parse("2022-01-01");
            Date b = simpleDateFormat.parse("2021-12-20");

            subDate(a, b);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void subDate(Date a, Date b) {
        long aTime = a.toInstant().toEpochMilli();
        long bTime = b.toInstant().toEpochMilli();
        System.out.println(bTime - aTime > 0 ? ((bTime - aTime) / (1000 * 60 * 60 * 24)) : (aTime - bTime) / (1000 * 60 * 60 * 24));
    }
}
